package com.oscarmovie.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oscarmovie.model.Paging;
import com.oscarmovie.model.Person;

public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {

	
	public void save(Person person) {
		getSession().saveOrUpdate(person);
	}

	public Person load(int id) {
		Person person = (Person)getSession().load(Person.class, id);
		return person;
	}
	
	
	public List<Person> loadAll() {
		Query query = getSession().createQuery("from Person person order by name");
		query.setCacheable(true);
		List<Person> list = (List<Person>)query.list();
		return list;
	}
	
	public List<Person> list(Paging paging) {
		paging.setTotalCount((int) count());
		Query query = getSession().createQuery("from Person person order by name");
		query.setCacheable(true);
		query.setFirstResult(paging.getCurrentRow()-1);
		query.setMaxResults(paging.getPageSize());
		List<Person> list = (List<Person>)query.list();
		return list;
	}
	private long count() {
		Query query = getSession().createQuery("select count(*) from Person");
		query.setCacheable(true);
		Long count = (Long) query.uniqueResult();
		return count;
	}

	
	public List<Person> loadAll(Paging paging, Person person) {
		paging.setTotalCount((int) count());
		Query query = getSession().createQuery("from Person person where person.name like :name or person.nameCn like :nameCn order by id desc");
		query.setCacheable(true);
		query.setString("name", "%"+person.getName()+"%");
		query.setString("nameCn", "%"+person.getNameCn()+"%");
		query.setFirstResult(paging.getCurrentRow()-1);
		query.setMaxResults(paging.getPageSize());
		List<Person> list = (List<Person>)query.list();
		return list;
	}
	
	
	public List<Person> loadAll(Person person) {
		Query query = getSession().createQuery("from Person person where person.name like :name or person.nameCn like :nameCn order by id desc");
		query.setCacheable(true);
		query.setString("name", "%"+person.getName()+"%");
		query.setString("nameCn", "%"+person.getNameCn()+"%");
		List<Person> list = (List<Person>)query.list();
		return list;
	}

	
	public List<Person> loadMostAwarded(int count) {
		Query query = getSession().createQuery("from Person person order by person.statistics.awarded desc");
		query.setCacheable(true);
		query.setFirstResult(0);
		query.setMaxResults(count);
		List<Person> list = (List<Person>)query.list();
		return list;
	}

	
	public List<Person> loadMostFavorite(int count) {
		Query query = getSession().createQuery("from Person person order by person.statistics.favorite desc");
		query.setCacheable(true);
		query.setFirstResult(0);
		query.setMaxResults(count);
		List<Person> list = (List<Person>)query.list();
		return list;
	}

	
	public List<Person> loadMostScore(int count) {
		Query query = getSession().createQuery("from Person person order by person.statistics.score desc");
		query.setCacheable(true);
		query.setFirstResult(0);
		query.setMaxResults(count);
		List<Person> list = (List<Person>)query.list();
		return list;
	}
}
